package com.wectrls.config;

import com.wectrls.common.utils.DateUtils;
import com.wectrls.properties.Swagger2ConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;


@EnableSwagger2
@Configuration
public class Swagger2Config {

    @Autowired
    private Swagger2ConfigProperties swagger2ConfigProperties;

    @Value("${spring.profiles.active:default}")
    private String active;

    @Bean
    public Docket createRestApi() {
        //配置扫描路径
      /*  ArrayList<Parameter> parameters = new ArrayList<>();

        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("Authorization")
                .description("请求Token")
                .modelRef(new ModelRef("String"))
                .parameterType("header")
                .scalarExample("Bearer ")
                .required(false)
                .build();
        parameters.add(parameterBuilder.build());*/
        //配置扫描路径
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger2ConfigProperties.getShowAllow())
                .host(swagger2ConfigProperties.getHost())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger2ConfigProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
//                .globalOperationParameters(parameters)
                .apiInfo(apiInfo())
                .securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()));
        return docket;
    }

    //Swagger2基础属性配置
    private ApiInfo apiInfo() {
        ApiInfoBuilder builder = new ApiInfoBuilder();
        builder.contact(new Contact("Tc2952", "http://baidu.com/", "282762259@qq.com"));
        //标题
        builder.title(swagger2ConfigProperties.getTitle() + "-" + active.toUpperCase());
        builder.version(swagger2ConfigProperties.getVersion());
        //描述
        builder.description(swagger2ConfigProperties.getDesc() + "-" + DateUtils.getFormatDate());
        //服务地址
        builder.termsOfServiceUrl(swagger2ConfigProperties.getTermUrl());
        //版本
        return builder.build();
    }

    /**
     * 这个类决定了你使用哪种认证方式，我这里使用密码模式
     * 其他方式自己摸索一下，完全莫问题啊
     */
    private SecurityScheme securityScheme() {
        GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("/form/token");
        OAuth oAuth = new OAuthBuilder()
                .name("xrlp")
                .grantTypes(Collections.singletonList(grantType))
                .scopes(Arrays.asList(scopes()))
                .build();
        return oAuth;
    }

    /**
     * 这里设置 swagger2 认证的安全上下文
     */
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(new SecurityReference("xrlp", scopes())))
                .forPaths(PathSelectors.any())
                .build();
    }

    /**
     * 这里是写允许认证的scope
     */
    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{
                new AuthorizationScope("all", "All scope is trusted!")
        };
    }

}
